package com.meilai.project.mapper.intentionScheme;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.business.intentionScheme.IntentionSchemeListDTO;
import com.meilai.project.entity.business.intentionScheme.IntentionScheme;
import com.meilai.project.vo.business.intentionScheme.AmendContractCountVO;
import com.meilai.project.vo.business.intentionScheme.DateRangeMediaTypeVO;
import com.meilai.project.vo.business.intentionScheme.IntentionSchemeVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author xchen
 * @since 2022-02-27
 */
@Mapper
public interface IntentionSchemeMapper extends BaseMapper<IntentionScheme> {

    @Update("update intention_scheme set related_id = #{newContractId} where related_type = 1 and related_id = #{oldContractId}")
    void updateContractId(@Param("oldContractId") Long oldContractId, @Param("newContractId") Long newContractId);

    @Select(" select ci.name as customer_name, ins.* from intention_scheme ins" + " join customer_info ci on ins.customer_id = ci.id" + " where ins.id = #{id} ")
    IntentionSchemeVO queryById(@Param("id") Long id);

    @Select(" select * from intention_scheme ins " + " join bill_sale bs on bs.choose_id = ins.choose_id " + " where bs.id = #{billSaleId} ")
    IntentionScheme getByBillSaleId(@Param("billSaleId") Long billSaleId);

    @Select("select * from intention_scheme ins where ins.choose_id = #{chooseId}")
    IntentionSchemeVO queryByChooseId(@Param("chooseId") Long chooseId);

    @Update("update intention_scheme set process_status = #{status} where choose_id = #{chooseId}")
    void updateLockStatus(@Param("chooseId") Long chooseId, @Param("status") Integer status);

    List<IntentionSchemeVO> listByRelated(@Param("related_id") Long related_id, @Param("related_type") Integer related_type, @Param("type") Integer type);

    IntentionSchemeVO getOneById(@Param("id") Long id);

    List<IntentionSchemeVO> listAllByRelatedType(@Param("related_type") Integer related_type);


    List<AmendContractCountVO> listByContractIds(@Param("ids") List<Long> contractIds);

    List<IntentionSchemeVO> listByCondition(Page<IntentionSchemeVO> page, @Param("searchDTO") IntentionSchemeListDTO searchDTO);

    List<IntentionSchemeVO> listByContractId(@Param("contractId") Long contractId);

    int releaseLock(@Param("chooseIdList") List<Long> chooseIdList);

    @Select("select  " + "ci.name as customer_name " + ", u.name as lockUserName " + ", ins.plan_name " + ", ins.lock_count " + ", GROUP_CONCAT(CONCAT(DATE_FORMAT(mpcd.start_at,'%Y-%m-%d'),'~',DATE_FORMAT(mpcd.end_at,'%Y-%m-%d'))) as cycle_date " + ", ci.maintenance_staff_id " + "from intention_scheme ins " + "left join user u on u.id = ins.lock_user_id " + "join customer_info ci on ins.customer_id = ci.id " + "join media_point_choose_daterange mpcd on mpcd.choose_id = ins.choose_id " + "where ins.status = 1 AND ins.id=#{id}")
    IntentionSchemeVO getPushMsgData(@Param("id") Long id);

    List<DateRangeMediaTypeVO> queryMediaTypeListByDateRangeIds(@Param("ids") List<Long> choose_id);
}
